package com.example.demo.module.purchase.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.example.demo.common.service.impl.BaseServiceImpl;
import com.example.demo.entity.pojo.purchase.PurchaseListPoJo;
import com.example.demo.entity.pojo.purchase.PurchaseOrderPoJo;
import com.example.demo.module.purchase.mapper.PurchaseOrderMapper;
import com.example.demo.module.purchase.service.PurchaseListService;
import com.example.demo.module.purchase.service.PurchaseOrderService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;


@Service("purchaseOrderService")
public class PurchaseOrderServiceImpl extends BaseServiceImpl<PurchaseOrderMapper, PurchaseOrderPoJo> implements PurchaseOrderService {
    @Resource
    private PurchaseOrderMapper mapper;
    @Resource
    private PurchaseListService listService;

    @Override
    public List<PurchaseOrderPoJo> findList(PurchaseOrderPoJo poJo, Date startTime, Date endTime) {
        return mapper.selectList(getLambda(poJo, startTime, endTime));
    }

    @Override
    public Boolean deleteByIds(List<Long> ids) {
        removeByIds(ids);
        PurchaseListPoJo poJo=new PurchaseListPoJo();
        ids.forEach(orderId->{
            poJo.setOrderId(orderId);
            poJo.setIsDelete(true);
            if(orderId!=null) {
                listService.remove(new LambdaUpdateWrapper<PurchaseListPoJo>().eq(PurchaseListPoJo::getOrderId, orderId));
            }
        });
        return null;
    }


    private LambdaQueryWrapper<PurchaseOrderPoJo> getLambda(PurchaseOrderPoJo poJo, Date startTime, Date endTime) {
        Long id = poJo.getId();
        Long companyId = poJo.getCompanyId();
        Date createTime = poJo.getCreateTime();
        Date updateTime = poJo.getUpdateTime();
        Boolean isDelete = poJo.getIsDelete();
        String orderNum=poJo.getOrderNum();
        String enterprise=poJo.getEnterprise();
        Date submitTime=poJo.getSubmitTime();
        Long state=poJo.getState();
        String carbonCopy=poJo.getCarbonCopy();
        String approval=poJo.getApproval();

        LambdaQueryWrapper<PurchaseOrderPoJo> wrapper = new LambdaQueryWrapper<PurchaseOrderPoJo>();
        wrapper
                .eq(id != null, PurchaseOrderPoJo::getId, poJo.getId())
                .eq(companyId != null, PurchaseOrderPoJo::getCompanyId, poJo.getCompanyId())
                .eq(createTime != null, PurchaseOrderPoJo::getCreateTime, poJo.getCreateTime())
                .eq(updateTime != null, PurchaseOrderPoJo::getUpdateTime, poJo.getUpdateTime())
                .eq(isDelete != null, PurchaseOrderPoJo::getIsDelete, poJo.getIsDelete())
                .eq(orderNum != null, PurchaseOrderPoJo::getOrderNum, poJo.getOrderNum())
                .eq(enterprise != null, PurchaseOrderPoJo::getEnterprise, poJo.getEnterprise())
                .eq(submitTime != null, PurchaseOrderPoJo::getSubmitTime, poJo.getSubmitTime())
                .eq(state != null, PurchaseOrderPoJo::getState, poJo.getState())
                .eq(carbonCopy != null, PurchaseOrderPoJo::getCarbonCopy, poJo.getCarbonCopy())
                .eq(approval != null, PurchaseOrderPoJo::getApproval, poJo.getApproval())
                .ge(startTime != null, PurchaseOrderPoJo::getSubmitTime, startTime)
                .le(endTime != null, PurchaseOrderPoJo::getSubmitTime, endTime);

        return wrapper;
    }
}